/**
 * Created by Zfy on 2015/11/3.
 */
function updateModal(name){
    $("#role_error").empty();
    $("#biaoti").html("编辑角色");
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"name":name},
        url:"/index.php?r=rbac/getrole",
        success:function(data) {
            if(data!=false){
                var data=eval(data);
                $("#role_description").val(data['description']);
                $("input[name=role_level]").each(function(a,b){
                    if( $(b).val()==data['role_level']){
                        $(b).attr("checked",true);
                    }
                });
                $("#role_name").val(data['name']);
                var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                treeObj.checkAllNodes(false);
                var nodes = treeObj.transformToArray(treeObj.getNodes());
                //$("#role_level").attr("checked",true);
                for(var i=0;i<data['permission'].length;i++){
                    for (var k=0; k< nodes.length; k++) {
                        if(nodes[k].id==data['permission'][i].replace('/',"|")){
                            treeObj.checkNode(nodes[k], true, true);
                        }
                    }
                }
                $("#role_level_div").hide();
                $("#addRoleFrom"). attr("action","/index.php?r=rbac/updaterole");
                $("#addRole").modal();
                $("#role_click").click(function(){
                    submitForm();
                });
            }
        }
    });
}
function addRole(){
    $("#biaoti").html("新增角色");
    $("#role_description").val('');
    $("#role_name").val('');
    $("input[name=role_level]").each(function(a,b){
        if( $(b).val()==3){
            $(b).attr("checked",true);
        }
    });

     var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                treeObj.checkAllNodes(false);
    $("#role_name").val('');
    $("#addRoleFrom"). attr("action","/index.php?r=rbac/addrole");
    $("#role_error").empty();
    $("#role_level_div").show();
    $("#addRole").modal();
    $("#role_click").click(function(){
        submitForm("add");
    });
}
function submitForm(type){
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
    var nodes = treeObj.getCheckedNodes(true);
    for (var i=0; i< nodes.length; i++) {
        name1=nodes[i].id.replace('|',"/")
        $("#check_pe").append("<input name='check[]' value='"+name1+"'/>");
    }
    $("#role_error").empty();
    var role_description=$("#role_description").val();
    if(role_description==''||role_description==null){
        $("#role_error").append("<p style='color: red'>角色名不能为空</p>");
        return false;
    }
    var pattern=/[`~!@#\$%\^\&\*\(\)_\+<>\?:"\{\},\.\\\/;'\[\]]/im;
    if(pattern.test(role_description)||role_description.length<4||role_description.length>20){
        $("#role_error").append("<p style='color: red'>角色名在4-20字符且不含特殊字符</p>");
        return false;
    }
    var role_level=$("input[name='role_level']:checked").val();
    if(type=="add"){
        if(role_level==''||role_level==null){
            $("#role_error").append("<p style='color: red'>配置一个角色层级</p>");
            return false;
        }
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{"role_description":role_description,'role_level':role_level},
            url:"/index.php?r=rbac/checkrolename",
            success:function(data) {
                $("#role_error").empty();
                if(data==true){
                    $("#addRoleFrom").submit();
                }else{
                    $("#role_error").append("<p style='color: red'>角色名在此层级已存在</p>");
                    return false;
                }
            }
        });
    }else{
       var  role_name=$("#role_name").val();
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{"role_description":role_description,'role_level':role_level,'role_name':role_name},
            url:"/index.php?r=rbac/checkrolename",
            success:function(data) {
                $("#role_error").empty();
                if(data==true){
                    $("#addRoleFrom").submit();
                }else{
                    $("#role_error").append("<p style='color: red'>角色名在此层级已存在</p>");
                    return false;
                }
            }
        });

    }


   /* if($("input[name='check[]']:checked").length>0){

    }else{
        $("#role_error").append("<p style='color: red'>请为该角色至少配置一项权限</p>");
        return false;
    }*/
}
function deleteModal(name){
    $("#del_error").empty();
    $("#deleteModal").modal();
    $("#del_click").click(function(){
        $("#del_error").empty();
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{"name":name},
            url:"/index.php?r=rbac/delrole",
            success:function(data) {
                $("#del_error").empty();
                if(data==false){
                    $("#del_error").append("<span style='color: red'>操作失败</span>");
                }else{
                    if(data=="success"){
                        location.reload();
                    }else{
                        $("#del_error").append("<span style='color: red'>"+data+"</span>");
                    }
                }
            }
        });
    });
}
function select1(tab){
    $('#tab_'+tab+' input').each(function(a,b){
        if($('#tab_'+tab+' input:eq(0)').is(":checked")){
            $(b).attr("checked",true);
        }else{
            $(b).attr("checked",false);
        }
    });
}
function select2(tab,tr){
    var tr="#tr_"+tr;
    if($(tr+' input:eq(0)').is(":checked")){
        $('#tab_'+tab+'  input:eq(0)').attr("checked",true);
        $(tr+' input').each(function(a,b){
            $(b).attr("checked",true);
        });
    }else{
        $(tr+' input').each(function(a,b){
            $(b).attr("checked",false);
        });
    }
}
function select3(tab,tr){
    var tr="#tr_"+tr;
    $('#tab_'+tab+' input:eq(0)').attr("checked",true);
    $(tr+' input:eq(0)').attr("checked",true);
}

